package com.song.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.song.mapper.ClazzMapper;
import com.song.pojo.Clazz;
import com.song.pojo.ClazzQueryParam;
import com.song.pojo.PageResult;
import com.song.service.ClazzService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;

@Service
public class ClazzServiceIml implements ClazzService {
    @Autowired
    private ClazzMapper clazzMapper;
    @Override
    public PageResult<Clazz> getClazzs(ClazzQueryParam param) {
        PageHelper.startPage(param.getPage(),param.getPageSize());
        List<Clazz> clazzs = clazzMapper.getClazzs(param);
        clazzs.forEach(clazz ->{
            String status;
            if (clazz.getEndDate().isBefore(LocalDate.now())){
                status = "已结课";
            }else if (clazz.getBeginDate().isAfter(LocalDate.now())){
                status = "未开搬";
            }else {
                status = "已开班";
            }
            clazz.setStatus(status);
                }
        );
        Page<Clazz> p = (Page<Clazz>) clazzs;
        return new PageResult<Clazz>(p.getTotal(), p.getResult());
    }

    @Override
    public Integer deleteClazz(Integer id) {
       return clazzMapper.deleteClazzById(id);
    }

    @Override
    public Integer addClazz(Clazz clazz) {
        clazz.setCreateTime(LocalDateTime.now());
        clazz.setUpdateTime(LocalDateTime.now());
         return clazzMapper.addClazz(clazz);
    }

    @Override
    public Clazz getClazz(Integer id) {
     return clazzMapper.getClazzById(id);
    }

    @Override
    public Integer updateClazz(Clazz clazz) {
        clazz.setUpdateTime(LocalDateTime.now());
        return clazzMapper.updateClazzById(clazz);
    }

    @Override
    public List<Clazz> getAllClazz() {
        return clazzMapper.getAllClazz();
    }

}
